package top.infopub.mgr.ic.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.BeforeToolAttDao;
import top.infopub.mgr.ic.dao.BeforeToolDao;
import top.infopub.mgr.ic.dao.BeforeToolDetailDao;
import top.infopub.mgr.ic.domain.BeforeToolDomain;
import top.infopub.model.ic.BeforeTool;
import top.infopub.model.ic.BeforeToolAtt;
import top.infopub.model.ic.BeforeToolDetail;
import top.infopub.mgr.ic.service.BeforeToolService;
import top.infopub.mgr.ic.vo.BeforeToolVo;

@Service
public class BeforeToolServiceImpl extends GenericServiceImpl<BeforeTool, String> implements
		BeforeToolService {

	@Autowired
	private BeforeToolDao beforeToolDao;
	
	@Autowired
	private BeforeToolDetailDao beforeToolDetailDao;
	
	@Autowired
	private BeforeToolAttDao beforeToolAttDao;
	
	@Autowired
	private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<BeforeTool, String> getDao() {
		return beforeToolDao;
	}

    @Override
    public Page<BeforeToolDomain> selectByDomainWhere(Page<BeforeToolDomain> page,
                                                      BeforeToolVo beforeToolVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BeforeToolDomain>)beforeToolDao.selectByDomainWhere(beforeToolVo);
    }

    @Override
    public Page<BeforeToolDomain> selectMyCheckByDomainWhere(Page<BeforeToolDomain> page,
                                                             BeforeToolVo beforeToolVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BeforeToolDomain>)beforeToolDao.selectMyCheckByDomainWhere(beforeToolVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBeforeTool(BeforeTool beforeTool,List<BeforeToolDetail> beforeToolDetails,List<BeforeToolAtt> beforeToolAtts) {

        beforeToolDao.insertById(beforeTool);
        
        if(CollectionUtils.isNotEmpty(beforeToolDetails)){
            beforeToolDetailDao.insertByBatch(beforeToolDetails);
        }
        
        if(CollectionUtils.isNotEmpty(beforeToolAtts)){
            beforeToolAttDao.insertByBatch(beforeToolAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.IC_BEFORE_TOOL;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, beforeTool.getId(), beforeTool.getProcessId(), null);
        
    }

    @Override
    public BeforeToolVo queryById(String id) {
        return beforeToolDao.queryById(id);
    }

}